﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//开摄像头的命名空间
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
using System.IO;
//语音命名空间
using System.Speech.Synthesis;

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 打开摄像头
    /// 2019-2-20
    /// </summary>
    public partial class FrmCamara : Form
    {
        public FrmCamara()
        {
            InitializeComponent();
        }

        //初始化
        FilterInfoCollection videoDevices;  //设备对象  可以是多个
        VideoCaptureDevice videoSource;     //网络摄像头对象
        public int selectedDeviceIndex = 0;

        //语音对象
        SpeechSynthesizer hello = new SpeechSynthesizer();

        //加载摄像头
        private void FrmCamara_Load(object sender, EventArgs e)
        {
            //实例化设备对象
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count == 0)
            {
                throw new ApplicationException();
            }

            foreach (FilterInfo item in videoDevices)
            {
                //加载设备到下拉框中
                this.comboBox1.Items.Add(item.Name);
            }

            this.comboBox1.SelectedIndex = 0;
        }

        //打开摄像头事件
        private void btnOpen_Click(object sender, EventArgs e)
        {

            hello.Speak("正在打开摄像头 请稍后");
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            selectedDeviceIndex = 0;
            //连接摄像头
            videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);

            videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];

            this.videoSourcePlayer1.VideoSource = videoSource;

            this.videoSourcePlayer1.Start();
        }

        //关闭摄像头事件
        private void btnClose_Click(object sender, EventArgs e)
        {
            if (videoSource == null)
            {
                MessageBox.Show("请先打开摄像头");
            }
            else
            {
                this.videoSourcePlayer1.SignalToStop();
                this.videoSourcePlayer1.WaitForStop();
            }
        }


        //拍照事件
        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (videoSource == null)
            {
                MessageBox.Show("请先开启摄像头");
                return;
            }

            hello.Speak("正在拍照 请稍后");
            Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
            string fileName = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".jpg";

            try
            {
                //保存到当前debug目录下
                bitmap.Save(Application.StartupPath + @"/img/" + fileName, ImageFormat.Jpeg);
                bitmap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //关闭窗体同时关闭摄像头
        private void FrmCamara_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.videoSourcePlayer1.SignalToStop();
            this.videoSourcePlayer1.WaitForStop();
        }
    }
}
